package com.jlf.starter.mp;

import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置类
 *
 * @author wujr
 * 2023/7/5
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/5 1.0 新增]
 */
@Configuration
public class MybatisAutoConfiguration {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }

    @Bean
    public ISqlInjector iSqlInjector() {
        return new DomainDefaultSqlInjector();
    }

    @Bean("autofillObjectHandler")
    public AutofillMetaObjectHandler metaObjectHandler(){
        return new AutofillMetaObjectHandler();
    }

//    @Bean(name = "globalConfig")
//    public GlobalConfig globalConfig(AutofillMetaObjectHandler autofillObjectHandler) {
//        GlobalConfig globalConfig = new GlobalConfig();
//        globalConfig.setMetaObjectHandler(autofillObjectHandler);
//        return globalConfig;
//    }
//    @Bean(name = "sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig,
//                                               MybatisPlusInterceptor mybatisPlusInterceptor)throws Exception{
//        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
//        factoryBean.setPlugins(mybatisPlusInterceptor);
//        factoryBean.setDataSource(dataSource);
//
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
//        configuration.setJdbcTypeForNull(JdbcType.NULL);
//        factoryBean.setConfiguration(configuration);
//        factoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
//        factoryBean.setGlobalConfig(globalConfig);
//
//        return factoryBean.getObject();
//    }
}
